package com.fangshi.api

import com.google.gson.annotations.SerializedName
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.Body
import retrofit2.http.GET
import retrofit2.http.POST
import retrofit2.http.Query

interface RemoteService {

    @GET("/apiApp/industryCellar/roomApi/room/graph")
    suspend fun getGarages(): GaragesResponse

    @POST("/apiApp/industryCellar/inOut/inbound")
    suspend fun exitOrEntry(@Body req: ExitEnterGarageRequest): Response

    @GET("/apiApp/industryCellar/inOut/getInOutHistory")
    suspend fun getRecords(
        @Query("pageNo") pageNo: Int,
        @Query("pageSize") pageSize: Int
    ): RecordsResponse

    companion object {

        fun create(baseUrl: String): RemoteService {
            val logger =
                HttpLoggingInterceptor().apply { level = HttpLoggingInterceptor.Level.BASIC }

            val client = OkHttpClient.Builder()
                .addInterceptor(logger)
                .hostnameVerifier(SSLFactory.getHostnameVerifier())
                .sslSocketFactory(SSLFactory.getSSLSocketFactory(), SSLFactory.getTrustManager()[0])
                .build()

            return Retrofit.Builder()
                .baseUrl(baseUrl)
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(RemoteService::class.java)
        }
    }
}

open class Response {
    @SerializedName("success")
    val success: Boolean = false

    @SerializedName("msg")
    val msg: String = "msg"
}

